package com.wzdigit.wms.common.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Map;

public class HttpsUtil {

	// http post
	public static Map<String, Object> httpRequest(String jsonParam, String url) {
		String json = "";
		try {
			StringEntity stringEntity = new StringEntity(jsonParam.toString(), "utf-8");
			stringEntity.setContentEncoding("UTF-8");
			stringEntity.setContentType("application/json");

			HttpPost httpPost = new HttpPost(url);
			httpPost.setEntity(stringEntity);
			httpPost.setHeader("Content-type", "application/json");

			CloseableHttpClient client = HttpClients.createDefault(); // 创建httpclient对象
			CloseableHttpResponse response = client.execute(httpPost); // 执行请求操作，并拿到结果（同步阻塞）
			HttpEntity httpEntity = response.getEntity(); // 获取结果实体
			if (httpEntity != null) {
				json = EntityUtils.toString(httpEntity, "UTF-8"); // 按指定编码转换结果实体为String类型
			}
			EntityUtils.consume(httpEntity);
			response.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSON.parseObject(json);
	}
}
